package com.qiumo.gmtool.config;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


/**
 * Gm相关配置
 */
@Data
@Component
@Slf4j
public class GenshinConfig implements CommandLineRunner {

    /**
     * 验证码有效时间
     */
    @Value("${genshin.expiretime}")
    private Integer expireTime;

    @Value("${genshin.keyPath}")
    public String keyPath;

    @Value("${genshin.region}")
    public String region;


    /**
     * 验证码缓存器uid：验证码
     */
    public static TimedCache<String, String> codeCache = null;


    /**
     * 获取一些必须的参数，避免重复put
     */
    public Map<String, Object> getParamsMap() {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("cmd", "1005");
        paramsMap.put("region", region);
        paramsMap.put("is_collectible", "False");
        paramsMap.put("item_limit_type", "2");
        return paramsMap;
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("----------------------开启缓存定时清理");
        codeCache = CacheUtil.newTimedCache(expireTime * 60 * 1000);
        codeCache.schedulePrune(1 * 60 * 1000);
        log.info("----------------------已开启");
    }
}
